import { configsData } from './configsdata'
import { parseGptCode, Googletag } from './adtype/googletag';
import { GptSlotDiv } from './adtype/gptslotdiv';
import { IRKlan, parseIRklanCode } from './adtype/irklan';
import { Pubguru, parsePubguruCode } from './adtype/pubguru';
import { parseAdSenseCode, AutoAdsense } from './adtype/autoadsense';
const AdTypeBuilder = {
  googletag: Googletag,
  gptslotdiv: GptSlotDiv,
  irklan: IRKlan,
  pubguru: Pubguru,
  autoadsense: AutoAdsense,
};

export function buildConfig({ host, dirpath, templateName, language }) {
  if (!host || !templateName) return ''

  const domainParts = host.trim().split('.')
  if (domainParts.length < 2 || !domainParts[1]) return ''

  const sitename = domainParts[1]
  const siteNameWithUppercase = sitename.charAt(0).toUpperCase() + sitename.slice(1)
  const template = JSON.parse(JSON.stringify(configsData[templateName]))

  const newConfig = {
    host: host.trim(),
    data: {
      name: siteNameWithUppercase,
      logo: `${templateName}/${sitename}`
    },
    html: template.html
  }

  if (language === 'kr' || language === 'jp') {
    newConfig.data.language = language
  }

  if (dirpath && dirpath.trim() !== '') {
    newConfig.data.dirpath = dirpath.trim()
  }

  function formatConfigObject(obj, indent = 1) {
    const tabs = '\t'.repeat(indent)
    let configString = ''
    const keys = Object.keys(obj)
    const lastKeyIndex = keys.length - 1

    keys.forEach((prop, index) => {
      const value = obj[prop]
      if (typeof value === 'object' && value !== null) {
        configString += `${tabs}${prop}: {\n${formatConfigObject(value, indent + 1)}${tabs}},`
      } else {
        configString += `${tabs}${prop}: \`${value}\`,`
      }
      if (index !== lastKeyIndex) configString += '\n'
    })

    return configString
  }

  return `{\n${formatConfigObject(newConfig)}\n},`
}

export function fillINPUT(configStr, htmlData, type) {
  const getHandlers = AdTypeBuilder[type] || (html => html);
  const newHtml = getHandlers(htmlData);
  let result = configStr;
  for (const key in newHtml) {
    const value = newHtml[key] || '';
    const pattern = new RegExp(`${key}:\\s*\`.*?\``, 'g');
    result = result.replace(pattern, `${key}: \`${value}\``);
  }
  return result;
}

export function getExampleForm(type) {
  const handler = AdTypeBuilder[type]
  if (handler && handler.exampleForm) {
    return JSON.parse(JSON.stringify(handler.exampleForm))
  }
  return null
}


export function parseAdCode(adCode) {
  if (adCode.includes('pagead2.googlesyndication.com/pagead/js/adsbygoogle.js')) {
    return parseAdSenseCode(adCode);
  } else if (!adCode.includes('adloox_pubint') &&
    (adCode.includes('securepubads.g.doubleclick.net/tag/js/gpt.js') ||
      adCode.includes('googletag.defineSlot'))
  ) {
    return parseGptCode(adCode);
  } else if (adCode.includes('adloox_pubint')) {
    return parseIRklanCode(adCode);
  } else if (adCode.includes('c.pubguru.net')) {
    return parsePubguruCode(adCode);
  }
  return { type: 'unknown' };
}

